package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: BaseAttrController
 * Package: com.atguigu.gmall.product.controller
 * Description:
 *
 * @Author ltystart
 * @Create 2025/8/21 12:05
 * @Version 1.0
 */
@Api(tags = "平台属性接口")
@RestController
@RequestMapping("/admin/product")
public class BaseAttrController {
    @Autowired
    private BaseAttrInfoService baseAttrInfoService;

    @Autowired
    private BaseAttrValueService baseAttrValueService;

    /**
     * 获取平台属性列表
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    @ApiOperation("获取平台属性列表")
    public Result getAttrInfoList(@PathVariable("category1Id") Long category1Id,
                                  @PathVariable("category2Id") Long category2Id,
                                  @PathVariable("category3Id") Long category3Id) {
        List<BaseAttrInfo> infos = baseAttrInfoService.getAttrInfoAndValue(category1Id, category2Id, category3Id);
        return Result.ok(infos);
    }

    /**
     * 保存平台属性
     *
     * @param baseAttrInfo
     * @return
     */
    @PostMapping("/saveAttrInfo")
    @ApiOperation("保存平台属性")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        if (baseAttrInfo.getId()== null){
            //属性新增
            baseAttrInfoService.saveAttrInfo(baseAttrInfo);
        }else {
            baseAttrInfoService.updateAttrInfo(baseAttrInfo);
        }

        return Result.ok();
    }

    /**
     * 根据属性id，查询出属性的所有值列表
     * @param attrId
     * @return
     */
    @GetMapping("/getAttrValueList/{attrId}")
    @ApiOperation("根据属性id，查询出属性的所有值列表")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId){
       List<BaseAttrValue> baseAttrValueList = baseAttrValueService.getAttrValueList(attrId);
       return Result.ok(baseAttrValueList);
    }
}
